package cc.mxtt.mvc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;

import cc.mxtt.common.core.BaseController;
import cc.mxtt.mvc.model.Dict;
import cc.mxtt.mvc.model.DictDetail;
import cc.mxtt.mvc.service.DictDetailService;
import cc.mxtt.mvc.service.DictService;
import cc.mxtt.mvc.service.impl.DictDetailServiceImpl;
import cc.mxtt.mvc.service.impl.DictServiceImpl;

public class DictController extends BaseController{
	DictService dictService =  new DictServiceImpl();
	DictDetailService dictDetailService = new DictDetailServiceImpl();
	
	public void index(){
		render("index.jsp");
	}
	public void list(){
		int pageNumber = getParaToInt("page",1);
		int pageSize = getParaToInt("rows",10);
		String text = getPara("text","");
		try {
			text = URLDecoder.decode(text, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Page<Dict> listdict = dictService.getListDict(pageNumber, pageSize, text);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", listdict.getTotalRow());
		result.put("rows", listdict.getList());
		renderJson(JsonKit.toJson(result));
	}
	public void detailList(){
		String dictid = getPara(0,"");
		if(!"".equals(dictid)){
			int pageNumber = getParaToInt("page",1);
			int pageSize = getParaToInt("rows",10);	
			Page<DictDetail> listdictdetail = dictDetailService.getListDictDetail(pageNumber, pageSize, dictid);
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("total", listdictdetail.getTotalRow());
			result.put("rows", listdictdetail.getList());
			renderJson(JsonKit.toJson(result));
		}
	}
	public void add(){
		Dict dict = new Dict();
		dict.set("code", getPara("code"));
		dict.set("text", getPara("text"));
		dict.set("status", getPara("status"));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if(dictService.create(dict)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	public void detailAdd(){
		DictDetail dictdetail = new DictDetail();
		dictdetail.set("code", getPara("code"));
		dictdetail.set("text", getPara("text"));
		dictdetail.set("val", getPara("val"));
		dictdetail.set("status", getPara("status"));
		dictdetail.set("dict_id", getPara(0));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if(dictDetailService.create(dictdetail)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	public void edit(){
		Dict dict = dictService.findById(getPara("id"));
		dict.set("code", getPara("code"));
		dict.set("text", getPara("text"));
		dict.set("status", getPara("status"));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if(dictService.update(dict)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	public void detailEdit(){
		DictDetail dictdetail = dictDetailService.findById(getPara("id"));
		dictdetail.set("code", getPara("code"));
		dictdetail.set("text", getPara("text"));
		dictdetail.set("val", getPara("val"));
		dictdetail.set("status", getPara("status"));
		dictdetail.set("dict_id", getPara(0));
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		if(dictDetailService.update(dictdetail)){
			result.put("flag", true);
		}else{
			result.put("flag", false);
		}
		renderJson(JsonKit.toJson(result));
	}
	public void delete(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String dictid = getPara("id");
		if(dictService.delete(dictid)){
			Msg = "数据字典信息删除成功！";
		}else{
			flag = false;
			Msg = "数据字典信息删除失败！";
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}
	public void detailDelete(){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag = true;
		String Msg = "";
		String id = getPara("id");
		if(dictDetailService.delete(id)){
			Msg = "数据字典选项信息删除成功！";
		}else{
			flag = false;
			Msg = "数据字典选项信息删除失败！";
		}
		result.put("flag", flag);
		result.put("Msg", Msg);
		renderJson(JsonKit.toJson(result));
	}

}
